package com.ttl.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttl.web.entity.Goods;
import com.ttl.web.entity.PageParam;
import com.ttl.web.entity.Category;
import com.ttl.web.entity.SelectType;
import com.ttl.web.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ttl.web.utils.ResultVo;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 商品分类管理
 */
@RestController
@RequestMapping("/api/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //新增
    @PostMapping
    public ResultVo add(@RequestBody Category category) {
        if (categoryService.save(category)) {
            return ResultVo.success("新增成功");
        }else {
            return ResultVo.error("新增失败");
        }
    }

    //编辑
    @PutMapping
    public ResultVo edit(@RequestBody Category category) {
        if (categoryService.updateById(category)) {
            return ResultVo.success("编辑成功");
        }else {
            return ResultVo.error("编辑失败");
        }
    }

    //删除
    @DeleteMapping("/{categoryId}")
    public ResultVo delete(@PathVariable Long categoryId) {
        if (categoryService.removeById(categoryId)) {
            return ResultVo.success("删除成功");
        }else {
            return ResultVo.error("删除失败");
        }
    }

    //查询
    @GetMapping
    public ResultVo getList(PageParam param) {
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getName())) {
            queryWrapper.like(Category::getCategoryName, param.getName()).orderByDesc(Category::getOrderNum);
        }
        // 创建分页对象
        Page<Category> page = new Page<>(Optional.ofNullable(param.getCurrentPage()).orElse(1), Optional.ofNullable(param.getPageSize()).orElse(10));
        // 执行分页查询
        IPage<Category> pageList = categoryService.page(page, queryWrapper);
        // 返回结果
        return ResultVo.success(pageList);
    }

    //小程序下拉数据查询
    @GetMapping("/getSelect")
    public ResultVo getSelect() {
        //查询所有分类数据
        List<Category> categoryList = categoryService.list();
        //组装需要的数据类型
        List<SelectType> selectTypeList = new ArrayList<>();
        Optional.ofNullable(categoryList).orElse(new ArrayList<>())
                .forEach(category -> {
            SelectType selectType = new SelectType();
            selectType.setLabel(category.getCategoryName());
            selectType.setValue(category.getCategoryId());
            selectTypeList.add(selectType);
        });
        return ResultVo.success("查询成功",selectTypeList);
    }

    //小程序分类查询
    @GetMapping("/getCateList")
    public ResultVo getCateList() {
        List<Category> list = categoryService.list();
        return ResultVo.success("查询成功",list);
    }

}
